package ch.bfh.swos.learnaid.rest.controller;

import java.util.Collection;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import ch.bfh.swos.learnaid.service.AnswerService;
import ch.bfh.swos.learnaid.service.dto.AnswerDTO;

@Controller
@RequestMapping("/answers")
public class AnswerController {

    @Inject
    private AnswerService answerService;

    /**
     * Create
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public AnswerDTO createAnswer(@RequestBody AnswerDTO answer) {
        AnswerDTO createdAnswer= answerService.create(answer);
        System.out.println("Answers created with id = " + createdAnswer.getId());
        return createdAnswer;
    }

    /**
     * ReadAll
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Collection<AnswerDTO> getAnswers() {
        System.out.println("Collection of Answers requested");
        return answerService.list();
    }

    /**
     * Read
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public AnswerDTO getAnswer(@PathVariable long id) {
        System.out.println("Answers requested with id = " + id);
        return answerService.read(id);
    }

    /**
     * Update
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    @ResponseBody
    public AnswerDTO updateAnswer(@RequestBody AnswerDTO answer, @PathVariable long id) {
        AnswerDTO updatedAnswer = answerService.update(answer);
        System.out.println("Answer updated with id = " + updatedAnswer.getId());
        return updatedAnswer;
    }

    /**
     * Delete
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void deleteAnswer(@PathVariable long id) {
        AnswerDTO answer = answerService.read(id);
        answerService.delete(answer);
        System.out.println("Delete Answer with id = " + id);
    }
}
